package com.example.sfm.controller;

import com.example.sfm.constant.MessageConstants;
import com.example.sfm.pojo.dto.*;
import com.example.sfm.result.Result;
import com.example.sfm.service.IUsersService;
import com.example.sfm.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@RestController("userUsersController")
@RequestMapping("/user/user")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "用户端相关接口", description = "用户端相关接口")
public class UsersController {
    private final IUsersService userService;

    /**
     * 更新用户信息
     */
    @Operation(summary = "更新用户信息", description = "根据传入的用户信息更新用户记录")
    @PutMapping("/update")
    public Result<?> updateUser(@RequestBody @Validated @Parameter(description = "用户信息更新参数") UserUpdateDto2 dto) {
        log.info("用户更新信息: {}", dto);
        userService.userUpdate(dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 分页查询用户
     */
    @Operation(summary = "分页查询用户", description = "根据查询条件分页查询用户列表")
    @GetMapping("/list")
    public Result<?> listUsers(@Parameter(description = "用户查询参数",in = ParameterIn.QUERY)UserQueryDto  dto) {
        log.info("查询用户列表: {}", dto);
        return Result.success(userService.listUsers(dto), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "用户注册", description = "用户提交注册信息进行注册")
    @PostMapping("/register")
    public Result<?> register(@RequestBody @Parameter(description = "用户注册信息") RegisterDto registerDto) {
        log.info("用户尝试注册: {}", registerDto);
        userService.register(registerDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "用户登录", description = "用户提交登录信息进行登录")
    @PostMapping("/login")
    public Result<?> login(@RequestBody @Parameter(description = "用户登录信息") LoginDto loginDto) {
        log.info("用户尝试登录: {}", loginDto);
        return Result.success(userService.login(loginDto), MessageConstants.LOGIN_SUCCESS);
    }

    @Operation(summary = "获取用户个人信息", description = "根据用户 ID 获取用户个人信息")
    @GetMapping("/info")
    public Result<?> getUserInfo() {
        Integer userId = ThreadLocalUtil.getId();
        log.info("用户获取个人信息, 用户 ID: {}", userId);
        return Result.success(userService.finById(userId), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "用户修改密码", description = "用户提交新密码进行密码修改")
    @PutMapping("/changePassword")
    public Result<?> changePassword(@RequestBody @Parameter(description = "用户修改密码信息") ChangePasswordDto changePasswordDto) {
        log.info("用户尝试修改密码:{}", changePasswordDto);
        userService.changePassword(changePasswordDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}